#include "ejres.h"
#include <QSplitter>
#include <QTextEdit>
#include <QTextCodec>
#include <QResizeEvent>

#include <QPushButton>
#include <QListWidget>
#include <QGraphicsItem>


#include <QDebug>

EjRes::EjRes(QWidget *parent) : QMainWindow(parent)
{
    //分割窗口
    auto mainSplitter = new QSplitter(Qt::Horizontal, this);

    m_sence = new QGraphicsScene();
    m_view = new QGraphicsView(m_sence, mainSplitter);
    m_view->show();

    connect(mainSplitter, SIGNAL(splitterMoved(int,int)), this, SLOT(on_change(int, int)));

    auto textItem = new QGraphicsTextItem("hello aaaaaaaaaaaaa!");
    textItem->setPos(0, 0);
    m_sence->addItem(textItem);
    m_sence->addLine(0,0,200,0);

    m_sence->addLine(0,0,0,200);

    m_sence->setBackgroundBrush(Qt::gray);

    //创建右分割窗口
    auto rightSplitter= new QSplitter(Qt::Vertical, mainSplitter);
    //设定分割窗口的分割条在拖动时是否为实时更新 true为实时更新 false则在拖动时只显示一条灰色的线条
    //在拖动到位并弹起鼠标后再显示分隔条
    rightSplitter->setOpaqueResize(false);
    //开始将其分割成上下两个窗口
    QTextEdit *upEdit = new QTextEdit(QObject::tr("上窗口"), rightSplitter);
    upEdit->setAlignment(Qt::AlignCenter);
    QTextEdit *bottomEdit = new QTextEdit(QObject::tr("下窗口"), rightSplitter);
    bottomEdit->setAlignment(Qt::AlignCenter);
    //设定可伸缩控件 第一个参数指定控件设置的控件序号，该控件序号按插入的先后次序依次从0开始编号
    //第二个参数大于0表示此控件为伸缩控件 此例中设置右部分割窗口为可伸缩控件
    mainSplitter->setStretchFactor(0, 1);
    setCentralWidget(mainSplitter);
}


void EjRes::resizeEvent(QResizeEvent *event)
{
    auto size = event->size();
    qDebug("w: %d, h:%d", size.width(), size.height());
    m_sence->setSceneRect(QRectF(2,10,size.width() - 265, size.height() - 20));
    m_senceSize = size;
}

void EjRes::on_change(int pos, int index)
{
    switch(index)
    {
    case 1:
        m_sence->setSceneRect(QRectF(1,1, pos, m_senceSize.height() - 20));
        break;
    }
}
